/*******************************************************************************/
/*                                                                             */
/*  Copyright (c) 2009: Peter Schregle,                                        */
/*  All rights reserved.                                                       */
/*                                                                             */
/*  This file is part of the Property Library.                                 */
/*                                                                             */
/*  Redistribution of the Property Library and use in source and binary forms, */
/*  with or without modification, are permitted provided that the following    */
/*  conditions are met:                                                        */
/*  1. Redistributions of source code must retain the above copyright notice,  */
/*     this list of conditions and the following disclaimer.                   */
/*  2. Redistributions in binary form must reproduce the above copyright       */
/*     notice, this list of conditions and the following disclaimer in the     */
/*     documentation and/or other materials provided with the distribution.    */
/*  3. Neither the name of Peter Schregle nor the names of other contributors  */
/*     may be used to endorse or promote products derived from this software   */
/*     without specific prior written permission.                              */
/*                                                                             */
/*  THIS SOFTWARE IS PROVIDED BY PETER SCHREGLE AND CONTRIBUTORS 'AS IS' AND   */
/*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE      */
/*  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE */
/*  ARE DISCLAIMED. IN NO EVENT SHALL PETER SCHREGLE OR CONTRIBUTORS BE LIABLE */
/*  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL */
/*  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS    */
/*  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)      */
/*  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,        */
/*  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN   */
/*  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE            */
/*  POSSIBILITY OF SUCH DAMAGE.                                                */
/*                                                                             */
/*******************************************************************************/


#ifndef __property_rwf_h__
#define __property_rwf_h__

#include <boost/operators.hpp>
#include <boost/function.hpp>

namespace pl {

/******************************************************************************/
/*                                                                            */
/* property_rwf                                                               */
/*                                                                            */
/******************************************************************************/

template <
	/// The type of the property.
	typename T>
/// A read-write property, backed by an internal field.
//!
//! You can read values from and write values to this property.
//!
//! The unit_steppable template takes and generates (==>) the following 
//! operators:
//! ++   ==>  ++(int), pre-increment versus post-increment, (incrementable),
//! --   ==>  --(int), pre-decrement versus post-decrement, (decrementable).
class property_rwf
	: boost::unit_steppable<pl::property_rwf<T>
	>
{
	/// The field that backs the property.
	T value_;

public:

	/// The value type of the property.
	typedef T value_type;

	/// Default construction of the property.
	//!
	//! By default, this initializes the property to zero (whatever that may
	//! mean for the type T). If you do not want this default-initialization
	//! because of performance reasons, you can get away with it, when you
	//! define the PL_NO_DEFAULT_INITIALIZATION macro.
	property_rwf()
#ifndef PL_NO_DEFAULT_INITIALIZATION
		: value_(T())
#endif
	{ }

	/// Construction of the property given an initial value.
	property_rwf(
		/// Initial value of the property.
		T const& value) 
		: value_(value)
	{ }

	/// Copy construction.
	property_rwf(pl::property_rwf<T> const& other)
		: value_(other.value_) 
	{ }

	/// Assign a new value to the property.
	pl::property_rwf<T> & operator =(
		/// The new value.
		pl::property_rwf<T> const& other)
	{
		value_ = other.value_;
		return *this;
	}

	/// Assign a new value to the property.
	pl::property_rwf<T> & operator =(
		/// The new value.
		T const& value)
	{
		value_ = value;
		return *this;
	}

	/// Member selection operator.
	//!
	//! This allows selection of a property of a property, i.e. chaining the
	//! access of properties.
	//!
	//! Overloading the dot (.) operator for this purpose would have been 
	//! preferred, but sadly the C++ language does not allow this.
	//!
	//! \return The address of this variable.
	T * operator ->()
	{
		return &value_;
	}

	/// Access the property value.
	//!
	//! \return The property value.
	operator T const& () const
	{
		return value_;
	}

	/// Access the property value.
	//!
	//! \return The property value.
	operator T & ()
	{
		return value_;
	}

	/// Increment.
	//!
	//! Through the use of boost::unit_steppable operator ++(int) - 
	//! post-increment - is also defined and implemented by calling this 
	//! operator.
	//!
	//! /return A reference to this object.
	pl::property_rwf<T> & operator ++()
	{
		++value_;
		return *this;
	}

	/// Decrement.
	//!
	//! Through the use of boost::unit_steppable operator --(int) - 
	//! post-decrement - is also defined and implemented by calling this 
	//! operator.
	//!
	//! /return A reference to this object.
	pl::property_rwf<T> & operator --()
	{
		--value_;
		return *this;
	}
};


} // namespace pl


#endif // __property_rwf_h__
